package com.example.fw;

import org.openqa.selenium.By; 

import com.example.tests.ContactData;

public class ContactHelper extends HelperBase {

	public ContactHelper(AppManager manager) {
		super(manager);
	}

	public void initContactCreation() {
		click(By.linkText("add new"));
	}

	public void fillContactCreationForm(ContactData contact) {
		type(By.name("firstname"), contact.firstName);
		type(By.name("lastname"), contact.lastName);
		type(By.name("address"), contact.address);
		type(By.name("home"), contact.homePhone);
		type(By.name("mobile"), contact.mobilePhone);
		type(By.name("work"), contact.workPhone);
		type(By.name("email"), contact.email);
		type(By.name("email2"), contact.email2);
	    selectByText(By.name("bday"), contact.bday);
	    selectByText(By.name("bmonth"), contact.bmonth);
		type(By.name("byear"), contact.byear);
		type(By.name("address2"), contact.address2);
		type(By.name("phone2"), contact.additionalPhone);
	}

	public void submitContactCreation() {
		click(By.name("submit"));
	}

	public void returntoHomePage() {
		click(By.linkText("home page"));
	}
	
    public void initContactModification(int index) {
        editContactByIndex(index);
    }

    private void editContactByIndex(int index) {
        int correctIndex = 1 + index;
        click(By.xpath("//*[@id='maintable']/tbody/tr[" + correctIndex + "]/td[7]/a/img"));
    }
    
    public void submitUpdate() {
        click(By.xpath("//*[@id='content']//*[@value='Update']"));
    }
    
	public void fillContactName(ContactData contact) {
		type(By.name("firstname"), contact.firstName);
		type(By.name("lastname"), contact.lastName);
	}
	
	public void deleteContact(int index){
        click(By.xpath("//*[@id='content']//*[@value='Delete']"));
	}
}
